import urllib3
import configparser
import sys


_cf = configparser.ConfigParser()


def _encode_process(procstr):

	nprocstr = procstr.replace(":", "_e1e_")
	nprocstr = nprocstr.replace(" ", "_e2e_")
	nprocstr = nprocstr.replace("/", "_e3e_")
	nprocstr = nprocstr.replace("?", "_e4e_")
	nprocstr = nprocstr.replace("=", "_e5e_")
	nprocstr = nprocstr.replace("&", "_e6e_")
	nprocstr = nprocstr.replace("|", "_e7e_")

	return nprocstr


def _req(param, isdecode):

	param["tc_formattype"] = "4"
	param["tc_mfuncno"] = "700"
	param["tc_sfuncno"] = "98"
	param["tc_service"] = "302"
	param["supportCompress"] = "18"
	param["op_station"] = "test"
	param["funcno"] = _encode_process(param["funcno"])

	http = urllib3.PoolManager()
	res = http.request("POST", _url, fields = param)

	if(isdecode):
		return res.data.decode("utf-8")
	else:
		return res.data

	# print(res.data)
	# print(res.data.decode("utf-8"))

# 格式化参数字符串
def _format_param(paramstr):

	rtn = {}
	paramarr = paramstr.split("&")
	
	for pairstr in paramarr:
		pairarr = pairstr.split("=")
		rtn[pairarr[0]] = pairarr[1]

	return rtn


# 获取参数配置
def _get_param(filename):

	_cf.read("func_params/" + filename + ".ini", "utf-8")
	items = _cf.items("param")

	rtn = {}
	for item in items:
		rtn[item[0]] = item[1]

	return rtn


# 后台地址

if __name__ == "__main__":

	param = _get_param("500.67")
	res = _req(param, True)
	print(res)